// 切换内容显示/隐藏
function toggleSection(id) {
    const content = document.getElementById(id);
    const icon = content.previousElementSibling.querySelector('i');
    
    if (content.style.display === "none" || content.style.display === "") {
        content.style.display = "block";
        icon.classList.remove('fa-chevron-down');
        icon.classList.add('fa-chevron-up');
    } else {
        content.style.display = "none";
        icon.classList.remove('fa-chevron-up');
        icon.classList.add('fa-chevron-down');
    }
}

// 初始化页面
document.addEventListener('DOMContentLoaded', function() {
    // 默认折叠部分内容
    const sections = ["education-content", "skills-content", "projects-content", "awards-content", "about-content"];
    sections.forEach(id => {
        const content = document.getElementById(id);
        const icon = content.previousElementSibling.querySelector('i');
        content.style.display = "none";
        icon.classList.remove('fa-chevron-up');
        icon.classList.add('fa-chevron-down');
    });
    
    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function(e) {
            e.preventDefault();
            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                target.scrollIntoView({
                    behavior: 'smooth',
                    block: 'start'
                });
                
                // 如果是折叠的内容，展开它
                const contentId = target.id + "-content";
                const content = document.getElementById(contentId);
                if (content && (content.style.display === "none" || content.style.display === "")) {
                    toggleSection(contentId);
                }
            }
        });
    });
    
    // 添加打印按钮
    const printBtn = document.createElement('button');
    printBtn.id = 'print-btn';
    printBtn.innerHTML = '<i class="fas fa-print"></i> <span>打印简历</span>';
    printBtn.style.backgroundColor = 'var(--primary-color)';
    printBtn.style.color = 'white';
    printBtn.style.border = 'none';
    printBtn.style.borderRadius = '5px';
    printBtn.style.cursor = 'pointer';
    printBtn.style.boxShadow = '0 2px 5px rgba(0,0,0,0.2)';
    printBtn.style.zIndex = '1000';
    printBtn.style.display = 'flex';
    printBtn.style.alignItems = 'center';
    printBtn.style.gap = '8px';
    printBtn.addEventListener('click', function() {
        // 打印前展开所有内容
        sections.forEach(id => {
            const content = document.getElementById(id);
            if (content.style.display === "none") {
                toggleSection(id);
            }
        });
        setTimeout(() => window.print(), 300);
    });
    document.body.appendChild(printBtn);
    
    // 导航栏高亮当前部分
    window.addEventListener('scroll', function() {
        const sections = document.querySelectorAll('section');
        const navItems = document.querySelectorAll('#navbar a');
        
        let current = '';
        sections.forEach(section => {
            const sectionTop = section.offsetTop;
            const sectionHeight = section.clientHeight;
            if (pageYOffset >= (sectionTop - 100)) {
                current = section.getAttribute('id');
            }
        });
        
        navItems.forEach(item => {
            item.classList.remove('active');
            if (item.getAttribute('href').includes(current)) {
                item.classList.add('active');
                item.style.backgroundColor = 'var(--accent-color)';
            } else {
                item.style.backgroundColor = '';
            }
        });
    });
});